package com.CGFrame.struts.bean.unit;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.configuration.Configuration;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Comments;
import com.CGFrame.struts.POJOs.Element;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Status;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.project.ProPathConst;
import com.config.GenericConfig;

public class UnitBean {
	/**获取根目录*/
	
	
	private String filebase = GenericConfig.getInstance().getFileBase();
	String type;
	int unitId;
	/**
	 * ��ʼ��
	 * @param type   Element or  Shot
	 * @param unitId   elementId or shotId
	 */
	public UnitBean(String type,int unitId)
	{
		this.type = type;
		this.unitId = unitId;
	}
	/**
	 * Set<Make>
	 * @return  Set���Unit�������������� Make
	 */
	public Set getExistMakes()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Object obj = new Object();
		if ( type.equals("Element") )
		{
			obj = pop.UniqueQuery("Element", "elementId,"+unitId);
			Element element = (Element ) obj;
			return element.getMakes();
		}
		else if ( type.equals("Shot"))
		{
			obj = pop.UniqueQuery("Shot", "shotId,"+unitId);
			Shot shot = (Shot) obj;
			return shot.getMakes();
		}
		else
		{
			System.out.println("UnitBean :Type Error");
			return null;
		}
	}
	
	/**
	 * �鿴Unit���Ƿ��Ѿ��� categoryId ��Ӧ�Ĺ�����
	 * @param categoryId
	 * @return  
	 */
	public boolean isExist(int categoryId)
	{
		Iterator ite_Makes = this.getExistMakes().iterator();
		while(ite_Makes.hasNext())
		{
			Make make = (Make)ite_Makes.next();
//			System.out.println(make.getCategory().getCategoryId());
//			System.out.println("Ҫ�ӵ�categoryId:"+categoryId);
			if( make.getCategory().getCategoryId().intValue() == categoryId) 
			{
//				System.out.println("exist");
				return true;
			}
		}
//		System.out.println("not exist");
		return false;	
	}
	/**
	 * 
	 * @param category
	 * @return Make ����category�������Unit�µ�����û�з���NUll
	 */
	public Make getMakeByCategory(Category category)
	{
		Iterator ite_Makes = this.getExistMakes().iterator();
		while(ite_Makes.hasNext())
		{
			Make make = (Make)ite_Makes.next();
			if ( make.getCategory().getCategoryId().compareTo(category.getCategoryId()) == 0 )
			{
				return make;
			}
		}
		return null;
	}
	/**
	 * Unit���������
	 * @param name   �������
	 * @param category   ��Ӧ��category
	 * @param ExceptHours  �ƻ�ʱ��
	 * @return make ��ӳɹ�����make�����ɹ�����NULL�����������ʧ��ԭ��
	 */
	public Make addMake(String name , Category category , int ExceptHours )
	{
		boolean b = createFile(type, name,unitId);
		if(!b){
			System.out.println("创建文件失败");
		}
		
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Make make = new Make();
		Object obj = new Object();
		if ( type.equals("Element") )
		{
			obj = pop.UniqueQuery("Element", "elementId,"+unitId);
			make.setElement( (Element)obj);
		}
		else if ( type.equals("Shot"))
		{
			obj = pop.UniqueQuery("Shot", "shotId,"+unitId);
			make.setShot( (Shot)obj );
		}
		if ( this.isExist(category.getCategoryId()) )
		{
//			System.out.println("\""+this.getUnitName()+"\"�Ѵ��ڹ���\""+category.getCategoryName()+"\" ��");
			return null;  //Unit has existed this category ,not add it
		}
		
		make.setCategory(category);
		make.setExceptHours(ExceptHours);
		make.setMakeName(name);
		Status status = (Status)pop.UniqueQuery("Status", "statusType,Make,statusName,HasInput");
		make.setStatus(status);  
		pop.saveObject(make);
		
		return make;
	}
	
	private boolean createFile(String type2, String name, int unitId2) {
		List lst = FilePathGenerator.getFilePath(unitId2, type2);
		String fs = System.getProperties().getProperty("file.separator");
		ProPathConst ppc = new ProPathConst();
		if(lst != null){
			StringBuffer path = (StringBuffer)lst.get(0);
			path.append(name).append(fs);
			String file1 = filebase + path + ppc.getParam11();
			String file2 = filebase + path + ppc.getParam12();
			mkdictory(file1);
			mkdictory(file2);
			return true;
		}else{
			return false;
		}
		
	}
	private File mkdictory(String fileName) {
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/**
	 * ȡUNIT����� 
	 * @return String UnitName
	 */
	public String getUnitName()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Object obj = new Object();
		if ( type.equals("Element") )
		{
			obj = pop.UniqueQuery("Element", "elementId,"+unitId);
			return ( (Element)obj ).getElementName();
		}
		else if ( type.equals("Shot"))
		{
			obj = pop.UniqueQuery("Shot", "shotId,"+unitId);;
			return ( (Shot)obj ).getShotName();
		}
		return "TYPE ERROR";
	}
	public Object getObject()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Object obj = new Object();
		if ( type.equals("Element") )
		{
			obj = pop.UniqueQuery("Element", "elementId,"+unitId);
			return obj;
		}
		else if ( type.equals("Shot"))
		{
			obj = pop.UniqueQuery("Shot", "shotId,"+unitId);;
			return obj;
		}
		return null;
	}

	public List getAllMakeStatus(){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		return pop.Query("Status","statusType,Make", null, null);
	}
	public void addComments(String reguserId, String comments) {
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Reguser reguser = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+reguserId);
		Comments comment = new Comments(reguser, this.unitId,this.type, new Date(),comments);
		pop.saveObject(comment);
		
	}
}
